package com.docMan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docMan.dao.BaseDao;
import com.docMan.dao.pojo.DocAdmin;
import com.docMan.dao.pojo.DocFtp;
import com.docMan.dao.pojo.Tchuman;
import com.docMan.service.DocAdminService;
import com.docMan.service.DocFtpService;
import com.docMan.service.TchumanService;
import com.docMan.utils.CastUtil;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.ProperiesUtils;

@Service("docAdminService")
public class DocAdminServiceImpl extends BaseServiceImpl<DocAdmin> implements DocAdminService {
	private DocFtpService docFtpService;
	private TchumanService tchumanService;
	private HttpSession session;
	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}


	@Resource(name="docFtpService")
	public void setDocFtpService(DocFtpService docFtpService) {
		this.docFtpService = docFtpService;
	}


	@Resource
	public void setTchumanService(TchumanService tchumanService) {
		this.tchumanService = tchumanService;
	}


	@Resource(name="docAdminDao")
	@Override
	public void setDao(BaseDao<DocAdmin> dao) {
		super.setDao(dao);
	}


	@Override
	public String getAdmins() {
		int humanId=(int) session.getAttribute(FinalConfig.SESSION_HUMANID);
		List<DocAdmin> docAdmins=findAll("");
		boolean flag =false;
		for (DocAdmin docAdmin : docAdmins) {
			if(docAdmin.getHumanId().equals(humanId))
				flag=true;
		}
		if(flag){
			List<Tchuman> tchumans=new ArrayList<Tchuman>();
			for (DocAdmin docAdmin : docAdmins) {
				Tchuman tchuman=tchumanService.getById(docAdmin.getHumanId());
				tchumans.add(tchuman);
			}
			DocFtp docFtp=docFtpService.getById(1);
			put("tchumans", tchumans);
			put("url", docFtp.getIp());
			put("username", docFtp.getUsername());
			put("password", docFtp.getPassword());
			put("port", docFtp.getPort());
			putSuccess();
		}else{
			putFalse();
			putMessage("你还不是管理员");
		}
		return getMap();
	}


	@Override
	public String saveAdmins(List<Integer> humanIds,String ip, String username, String password,
			String port) {
		putFalse();
		update("delete from DocAdmin where 1=1");
		if(null!=humanIds&&humanIds.size()>0){
			List<DocAdmin> list=new ArrayList<>();
			for (Integer integer : humanIds) {
				DocAdmin docAdmin=new DocAdmin();
				docAdmin.setHumanId(integer);
				list.add(docAdmin);
			}
			saveOrUpdateAll(list);
			putSuccess();
		}
		DocFtp docFtp=docFtpService.getById(1);
		docFtp.setIp(ip);
		docFtp.setUsername(username);
		docFtp.setPassword(password);
		docFtp.setPort(Integer.parseInt(port));
		return getMap();
	}


	@Override
	public String updateFtp(String ip, String username, String password,
			String port) {
		ProperiesUtils.setProperty(FinalConfig.FTP_IP, ip);
		ProperiesUtils.setProperty(FinalConfig.FTP_USERNAME, username);
		ProperiesUtils.setProperty(FinalConfig.FTP_PASSWORD, password);
		ProperiesUtils.setProperty(FinalConfig.FTP_PORT, port);
		return getMap();
	}


	@Override
	public String getFtp() {
		put("url", ProperiesUtils.getProperty(FinalConfig.FTP_IP));
		put("username", ProperiesUtils.getProperty(FinalConfig.FTP_USERNAME));
		put("password", ProperiesUtils.getProperty(FinalConfig.FTP_PASSWORD));
		put("port", ProperiesUtils.getProperty(FinalConfig.FTP_PORT));
		return getMap();
	}


	@Override
	public boolean isAdmin() {
		int humanId=CastUtil.castInt(session.getAttribute(FinalConfig.SESSION_HUMANID));
		List<DocAdmin> docAdmins=find("select admin from DocAdmin admin where admin.humanId=?",humanId);
		if(docAdmins.size()>0)
		{
			session.setAttribute(FinalConfig.is_admin, true);
			return true;
		}
		return false;
	}
	
}
